import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
    state:{
        prodcuts:[
            {
                name:'鼠标',
                price:20
            },
            {
                name:'键盘',
                price:40
            },
            {
                name:'显示屏',
                price:50
            },
            {
                name:'耳机',
                price:60
            }
        ]
    },
    mutations:{
        change(state,{index,num}){
            console.log(index)
            state.prodcuts[index].price -= num
        }
    },
    getters:{
        saleProduct(state){
            let saleArr =  state.prodcuts.map(item => {
                return {
                    name:item.name,
                    price:`￥${item.price}`
                }
            })
            return saleArr
        }
    },
    actions:{
        changeAsync(context,params){
            setTimeout(() => {
                context.commit('change',params)
            },2000)
        }
    }
})